package com.si.controller.manage;

import com.si.entity.*;
import com.si.service.*;
import com.si.util.IpAddressUtil;
import com.si.util.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户管理
 * @author x1c
 *
 */
@Controller
@RequestMapping("user/manage")
public class UserManageController {
	@Autowired
	private OperationLogService operationLogService;
	@Autowired
	private SysUserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuService menuService;
	/**
	 *
	 * @return
	 */
	@RequestMapping(value="userlist")
	public String userlist(ModelMap modelMap,
												 @RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
												 @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
		List<SysUser> all = userService.getAll();
		List<Integer> names = new ArrayList<>();//用来临时存储person的id

		List<SysUser> personList = all.stream().filter(// 过滤去重
						v -> {
							boolean flag = !names.contains(v.getId());
							names.add(v.getId());
							return flag;
						}
		).collect(Collectors.toList());
		modelMap.put("paging", personList);
		return "managePage/user/list";
	}
	/**
	 * 进入到添加页面
	 * @return
	 */
	@RequestMapping(value="userinitAdd")
	public String userinitAdd(ModelMap modelMap) {
		List<Role> menuall = roleService.getAll();
		List<Integer> names = new ArrayList<>();//用来临时存储person的id
		List<Role> personList = menuall.stream().filter(// 过滤去重
						v -> {
							boolean flag = !names.contains(v.getId());
							names.add(v.getId());
							return flag;
						}
		).collect(Collectors.toList());
		modelMap.put("role",personList);
		return "managePage/user/userupdate";
	}



	/**
	 * 进入到修改页面
	 * @return
	 */
	@RequestMapping(value="userupdate/{id}")
	public String userupdate(
					ModelMap modelMap,
					@PathVariable(value="id") Integer id) {
		SysUser info=(SysUser)userService.getObjectById(id);
		modelMap.put("user", info);
		//查询二级分类的信息
		List<Role> menuall = roleService.getAll();
		List<Integer> names = new ArrayList<>();//用来临时存储person的id
		List<Role> personList = menuall.stream().filter(// 过滤去重
						v -> {
							boolean flag = !names.contains(v.getId());
							names.add(v.getId());
							return flag;
						}
		).collect(Collectors.toList());
		modelMap.put("role",personList);
		return "managePage/user/userupdate";
	}

	/**
	 * 保存数据
	 * @return
	 */
	@RequestMapping(value="saveuser")
	public String usersave(HttpServletRequest request,
										 ModelMap modelMap,	 @RequestParam(value = "id") Integer id,
												 @RequestParam(value = "userName") String userName,
												 @RequestParam(value = "passWord") String passWord,
												 @RequestParam(value = "realname") String realname,
												 @RequestParam(value = "phoneNum") String phoneNum,
												 @RequestParam(value = "role") Integer role) {
		SysUser byId = (SysUser) userService.getObjectById(id);
		byId.setPassWord(passWord);
		byId.setPhoneNum(phoneNum);
		byId.setUserName(userName);
		Role byId1 = (Role) roleService.getObjectById(role);
		byId.setRole(byId1);
		byId.setRealname(realname);
		userService.saveOrUpdate(byId);
		return "redirect:/user/manage/userlist.html";
	}

	/**
	 * 根据id 删除
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="userdelete/{id}")
	public String userdelete(
					@PathVariable(value="id") Integer id,
					HttpServletRequest request,	RedirectAttributes redirectAttributes){
		SysUser info=(SysUser)userService.getObjectById(id);
		userService.delete(info);
		return "redirect:/user/manage/userlist.html";
	}


	/*角色开始👇*/

	/**
	 *
	 * @return
	 */
	@RequestMapping(value="rolelist")
	public String rolelist(ModelMap modelMap,
										 @RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
										 @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {

		List<Role> all = roleService.getAll();
		List<String> names = new ArrayList<>();//用来临时存储person的id

		List<Role> personList = all.stream().filter(// 过滤去重
						v -> {
							boolean flag = !names.contains(v.getName());
							names.add(v.getName());
							return flag;
						}
		).collect(Collectors.toList());

		modelMap.put("paging", personList);
		return "managePage/user/role";
	}
	/**
	 * 进入到添加页面
	 * @return
	 */
	@RequestMapping(value="roleinitAdd")
	public String initAdd(ModelMap modelMap) {
		List<Menu> menuall = menuService.getAll();
		modelMap.put("menu",menuall);
		return "managePage/user/roleupdate";
	}



	/**
	 * 进入到修改页面
	 * @return
	 */
	@RequestMapping(value="roleupdate/{id}")
	public String update(
					ModelMap modelMap,
					@PathVariable(value="id") Integer id) {
		Role info=(Role)roleService.getObjectById(id);
		modelMap.put("role", info);
		//查询二级分类的信息
		List<Menu> menuall = menuService.getAll();
		modelMap.put("menu",menuall);
		return "managePage/user/roleupdate";
	}

	/**
	 * 保存数据
	 * @return
	 */
	@RequestMapping(value="saverole")
	public String save(HttpServletRequest request,
										 ModelMap modelMap,
										 @RequestParam(value = "id") Integer id,
										 @RequestParam(value = "name") String name,
										 @RequestParam(value = "numbers") String numbers,
										 @RequestParam(value = "menu") Integer[] menu) {
		Role info=(Role)roleService.getObjectById(id);
		List<Menu> menus = new ArrayList<>();
		for (Integer integer : menu) {
			Menu byId = (Menu) menuService.getObjectById(integer);
			menus.add(byId);
		}
		info.setName(name);
		info.setNumbers(numbers);
		info.setMenuList(menus);
		roleService.saveOrUpdate(info);
		return "redirect:/user/manage/rolelist.html";
	}

	/**
	 * 根据id 删除
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="roledelete/{id}")
	public String delete(
					@PathVariable(value="id") Integer id,
					HttpServletRequest request,	RedirectAttributes redirectAttributes){
		Role info=(Role)roleService.getObjectById(id);
		roleService.delete(info);
		return "redirect:/user/manage/rolelist.html";
	}

}
